library(tidyr)
library(ggplot2)
library(lubridate)
library(dplyr)
library(zoo)
library(ggthemes)
library(readxl)
library(kableExtra)

svbbb <- read_excel("data/raw/sectarian_events_bbb.xlsx")

#reformat date to r readable
svbbb$date <- as.Date(svbbb$date)

#remove obs w/ missing for bbb categories
svbbb <- svbbb %>%
  filter(!is.na(bbb))

# select data 
dat_short <- svbbb %>% 
  select(bbb, date, dead)

# generate column of 1s to collapse by
dat_short$event <- 1

# hard to see daily so plot by month and year instead of date
dat_short$myear <- format(as.Date(svbbb$date), "%Y-%m")
dat_short$myear <- as.Date(as.yearmon(dat_short$myear))


dat_short_msums <- dat_short %>%
  group_by(myear) %>%
  summarize(sum_events = sum(event))

pdf("plots/Figure_2_Events.pdf")
ggplot(data = dat_short_msums,
       mapping = aes(x = myear, y = sum_events)) +
  geom_bar(stat = "identity") +
  theme_tufte() +
  theme(text=element_text(family="Helvetica", size = 12),
        axis.text.x = element_text(hjust = 1, vjust = 0.5, size=12)) +
  labs(x="Month", y="# Events", shape="Event type") +
  scale_y_continuous(breaks = scales::pretty_breaks(5)) +
  scale_x_date(breaks = seq(as.Date("2013-07-01"), as.Date("2019-07-01"), by = "1 year"), date_labels = "%B %Y")
dev.off()

dat_short_mdsums <- dat_short %>%
  group_by(myear) %>%
  summarize(sum_deaths = sum(dead))

pdf("plots/Figure_3_Deaths.pdf")
ggplot(data = dat_short_mdsums,
       mapping = aes(x = myear, y = sum_deaths)) +
  geom_bar(stat = "identity") +
  theme_tufte() +
  theme(text=element_text(family="Helvetica", size = 12),
        axis.text.x = element_text(hjust = 1, vjust = 0.5, size=12)) +
  labs(x="Month", y="# Deaths", shape="Event type") +
  scale_y_continuous(breaks = scales::pretty_breaks(5)) +
  scale_x_date(breaks = seq(as.Date("2013-07-01"), as.Date("2019-07-01"), by = "1 year"), date_labels = "%B %Y")
dev.off()